Remove command prototype
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 12 Oct 2016 11:39:46 +0000 (14:39 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 12 Oct 2016 11:39:46 +0000 (14:39 +0300)
src/cargo/ops/cargo_run.rs
src/cargo/ops/cargo_rustc/command.rs [deleted file]
src/cargo/ops/cargo_rustc/compilation.rs
src/cargo/ops/cargo_rustc/custom_build.rs
src/cargo/ops/cargo_rustc/job_queue.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/ops/cargo_test.rs
src/cargo/ops/mod.rs

index f994541cc02bf0188c2ee28d0c3f30daa49e8f55..34c28dbc4cbb8b3ee364fbe77d3fe9e7311b3bca 100644 (file)
@@ -48,8 +48,7 @@ pub fn run(ws: &Workspace,
         Some(path) => path.to_path_buf(),
         None => exe.to_path_buf(),
     };
-    let mut process = try!(compile.target_process(exe, &root))
-                                  .into_process_builder();
+    let mut process = try!(compile.target_process(exe, &root));
     process.args(args).cwd(config.cwd());
 
     try!(config.shell().status("Running", process.to_string()));
diff --git a/src/cargo/ops/cargo_rustc/command.rs b/src/cargo/ops/cargo_rustc/command.rs
deleted file mode 100644 (file)
index d49951c..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-use std::collections::HashMap;
-use std::ffi::{OsStr, OsString};
-use std::fmt;
-use std::path::Path;
-
-use util::{CargoResult, ProcessBuilder, process};
-use util::Config;
-
-/// Prototype for a command that must be executed.
-#[derive(Clone)]
-pub struct CommandPrototype {
-    ty: CommandType,
-    builder: ProcessBuilder,
-}
-
-impl CommandPrototype {
-    pub fn new(ty: CommandType, config: &Config)
-               -> CargoResult<CommandPrototype> {
-        Ok(CommandPrototype {
-            builder: {
-                let mut p = match ty {
-                    CommandType::Rustc => try!(config.rustc()).process(),
-                    CommandType::Rustdoc => process(&*try!(config.rustdoc())),
-                    CommandType::Target(ref s) |
-                    CommandType::Host(ref s) => process(s),
-                };
-                p.cwd(config.cwd());
-                p
-            },
-            ty: ty,
-        })
-    }
-
-    pub fn get_type(&self) -> &CommandType { &self.ty }
-
-    pub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut CommandPrototype {
-        self.builder.arg(arg);
-        self
-    }
-
-    pub fn args<T: AsRef<OsStr>>(&mut self, arguments: &[T]) -> &mut CommandPrototype {
-        self.builder.args(arguments);
-        self
-    }
-
-    pub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut CommandPrototype {
-        self.builder.cwd(path);
-        self
-    }
-
-    pub fn env<T: AsRef<OsStr>>(&mut self, key: &str, val: T)
-                                -> &mut CommandPrototype {
-        self.builder.env(key, val);
-        self
-    }
-
-    pub fn get_args(&self) -> &[OsString] { self.builder.get_args() }
-    pub fn get_cwd(&self) -> Option<&Path> { self.builder.get_cwd() }
-
-    pub fn get_env(&self, var: &str) -> Option<OsString> {
-        self.builder.get_env(var)
-    }
-
-    pub fn get_envs(&self) -> &HashMap<String, Option<OsString>> {
-        self.builder.get_envs()
-    }
-
-    pub fn into_process_builder(self) -> ProcessBuilder {
-        self.builder
-    }
-}
-
-impl fmt::Display for CommandPrototype {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        self.builder.fmt(f)
-    }
-}
-
-#[derive(Clone, Debug)]
-pub enum CommandType {
-    Rustc,
-    Rustdoc,
-
-    /// The command is to be executed for the target architecture.
-    Target(OsString),
-
-    /// The command is to be executed for the host architecture.
-    Host(OsString),
-}
index 23f0a044fcfc21ce7e90775e7ed467b574875e05..0c60751698f4c580122f2fa2c0a55ee4fe3c8d9e 100644 (file)
@@ -1,12 +1,10 @@
 use std::collections::{HashMap, HashSet};
-use std::ffi::OsStr;
+use std::ffi::{OsStr, OsString};
 use std::path::PathBuf;
 use semver::Version;
 
 use core::{PackageId, Package, Target};
-use util::{self, CargoResult, Config};
-
-use super::{CommandType, CommandPrototype};
+use util::{self, CargoResult, Config, ProcessBuilder, process};
 
 /// A structure returning the result of a compilation.
 pub struct Compilation<'cfg> {
@@ -49,6 +47,18 @@ pub struct Compilation<'cfg> {
     config: &'cfg Config,
 }
 
+#[derive(Clone, Debug)]
+pub enum CommandType {
+    Rustc,
+    Rustdoc,
+
+    /// The command is to be executed for the target architecture.
+    Target(OsString),
+
+    /// The command is to be executed for the host architecture.
+    Host(OsString),
+}
+
 impl<'cfg> Compilation<'cfg> {
     pub fn new(config: &'cfg Config) -> Compilation<'cfg> {
         Compilation {
@@ -67,25 +77,25 @@ impl<'cfg> Compilation<'cfg> {
     }
 
     /// See `process`.
-    pub fn rustc_process(&self, pkg: &Package) -> CargoResult<CommandPrototype> {
+    pub fn rustc_process(&self, pkg: &Package) -> CargoResult<ProcessBuilder> {
         self.process(CommandType::Rustc, pkg)
     }
 
     /// See `process`.
     pub fn rustdoc_process(&self, pkg: &Package)
-                           -> CargoResult<CommandPrototype> {
+                           -> CargoResult<ProcessBuilder> {
         self.process(CommandType::Rustdoc, pkg)
     }
 
     /// See `process`.
     pub fn target_process<T: AsRef<OsStr>>(&self, cmd: T, pkg: &Package)
-                                               -> CargoResult<CommandPrototype> {
+                                           -> CargoResult<ProcessBuilder> {
         self.process(CommandType::Target(cmd.as_ref().to_os_string()), pkg)
     }
 
     /// See `process`.
     pub fn host_process<T: AsRef<OsStr>>(&self, cmd: T, pkg: &Package)
-                                             -> CargoResult<CommandPrototype> {
+                                         -> CargoResult<ProcessBuilder> {
         self.process(CommandType::Host(cmd.as_ref().to_os_string()), pkg)
     }
 
@@ -95,7 +105,7 @@ impl<'cfg> Compilation<'cfg> {
     /// The package argument is also used to configure environment variables as
     /// well as the working directory of the child process.
     pub fn process(&self, cmd: CommandType, pkg: &Package)
-                   -> CargoResult<CommandPrototype> {
+                   -> CargoResult<ProcessBuilder> {
         let mut search_path = vec![];
 
         // Add -L arguments, after stripping off prefixes like "native=" or "framework=".
@@ -121,7 +131,12 @@ impl<'cfg> Compilation<'cfg> {
         search_path.extend(util::dylib_path().into_iter());
         let search_path = try!(util::join_paths(&search_path,
                                                 util::dylib_path_envvar()));
-        let mut cmd = try!(CommandPrototype::new(cmd, self.config));
+        let mut cmd = match cmd {
+            CommandType::Rustc => try!(self.config.rustc()).process(),
+            CommandType::Rustdoc => process(&*try!(self.config.rustdoc())),
+            CommandType::Target(ref s) |
+            CommandType::Host(ref s) => process(s),
+        };
         cmd.env(util::dylib_path_envvar(), &search_path);
         if let Some(env) = self.extra_env.get(pkg.package_id()) {
             for &(ref k, ref v) in env {
index 5a15e85a853af264e18350ffc4e99cd78e95f31e..73dd34e6dd261b9ccc9c33ff0cb51107a0e69c3d 100644 (file)
@@ -10,7 +10,7 @@ use util::{internal, ChainError, profile, paths};
 
 use super::job::Work;
 use super::{fingerprint, Kind, Context, Unit};
-use super::CommandType;
+use super::compilation::CommandType;
 
 /// Contains the parsed output of a custom build script.
 #[derive(Clone, Debug, Hash)]
@@ -98,30 +98,30 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
     // package's library profile.
     let profile = cx.lib_profile(unit.pkg.package_id());
     let to_exec = to_exec.into_os_string();
-    let mut p = try!(super::process(CommandType::Host(to_exec), unit.pkg, cx));
-    p.env("OUT_DIR", &build_output)
-     .env("CARGO_MANIFEST_DIR", unit.pkg.root())
-     .env("NUM_JOBS", &cx.jobs().to_string())
-     .env("TARGET", &match unit.kind {
-         Kind::Host => cx.host_triple(),
-         Kind::Target => cx.target_triple(),
-     })
-     .env("DEBUG", &profile.debuginfo.to_string())
-     .env("OPT_LEVEL", &profile.opt_level)
-     .env("PROFILE", if cx.build_config.release {"release"} else {"debug"})
-     .env("HOST", cx.host_triple())
-     .env("RUSTC", &try!(cx.config.rustc()).path)
-     .env("RUSTDOC", &*try!(cx.config.rustdoc()));
-
-     if let Some(links) = unit.pkg.manifest().links(){
-        p.env("CARGO_MANIFEST_LINKS", links);
-     }
+    let mut cmd = try!(super::process(CommandType::Host(to_exec), unit.pkg, cx));
+    cmd.env("OUT_DIR", &build_output)
+       .env("CARGO_MANIFEST_DIR", unit.pkg.root())
+       .env("NUM_JOBS", &cx.jobs().to_string())
+       .env("TARGET", &match unit.kind {
+           Kind::Host => cx.host_triple(),
+           Kind::Target => cx.target_triple(),
+       })
+       .env("DEBUG", &profile.debuginfo.to_string())
+       .env("OPT_LEVEL", &profile.opt_level)
+       .env("PROFILE", if cx.build_config.release { "release" } else { "debug" })
+       .env("HOST", cx.host_triple())
+       .env("RUSTC", &try!(cx.config.rustc()).path)
+       .env("RUSTDOC", &*try!(cx.config.rustdoc()));
+
+    if let Some(links) = unit.pkg.manifest().links() {
+        cmd.env("CARGO_MANIFEST_LINKS", links);
+    }
 
     // Be sure to pass along all enabled features for this package, this is the
     // last piece of statically known information that we have.
     if let Some(features) = cx.resolve.features(unit.pkg.package_id()) {
         for feat in features.iter() {
-            p.env(&format!("CARGO_FEATURE_{}", super::envify(feat)), "1");
+            cmd.env(&format!("CARGO_FEATURE_{}", super::envify(feat)), "1");
         }
     }
 
@@ -192,19 +192,18 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
                 }));
                 let data = &state.metadata;
                 for &(ref key, ref value) in data.iter() {
-                    p.env(&format!("DEP_{}_{}", super::envify(&name),
-                                   super::envify(key)), value);
+                    cmd.env(&format!("DEP_{}_{}", super::envify(&name),
+                                     super::envify(key)), value);
                 }
             }
             if let Some(build_scripts) = build_scripts {
-                try!(super::add_plugin_deps(&mut p, &build_state,
+                try!(super::add_plugin_deps(&mut cmd, &build_state,
                                             &build_scripts));
             }
         }
 
         // And now finally, run the build command itself!
-        state.running(&p);
-        let cmd = p.into_process_builder();
+        state.running(&cmd);
         let output = try!(cmd.exec_with_streaming(
             &mut |out_line| { state.stdout(out_line); Ok(()) },
             &mut |err_line| { state.stderr(err_line); Ok(()) },
index d02d841b828dd2d34cf7ae02ac14254f726ebd33..39d1c45a7852572de2265c0c11ce7efd20d81abe 100644 (file)
@@ -9,11 +9,10 @@ use term::color::YELLOW;
 
 use core::{PackageId, Target, Profile};
 use util::{Config, DependencyQueue, Fresh, Dirty, Freshness};
-use util::{CargoResult, profile, internal};
+use util::{CargoResult, ProcessBuilder, profile, internal};
 
 use super::{Context, Kind, Unit};
 use super::job::Job;
-use super::command::CommandPrototype;
 
 /// A management structure of the entire dependency graph to compile.
 ///
@@ -64,7 +63,7 @@ enum Message {
 }
 
 impl<'a> JobState<'a> {
-    pub fn running(&self, cmd: &CommandPrototype) {
+    pub fn running(&self, cmd: &ProcessBuilder) {
         let _ = self.tx.send((self.key, Message::Run(cmd.to_string())));
     }
 
index 1efcc2b0c1d320db2063c3052374e44309d13c6f..88328cb99110ad2fa82bf2d81ed2ded82ef24054 100644 (file)
@@ -10,19 +10,17 @@ use rustc_serialize::json;
 use core::{Package, PackageId, PackageSet, Target, Resolve};
 use core::{Profile, Profiles, Workspace};
 use core::shell::ColorConfig;
-use util::{self, CargoResult, human, machine_message};
+use util::{self, CargoResult, ProcessBuilder, human, machine_message};
 use util::{Config, internal, ChainError, profile, join_paths, short_hash};
 
 use self::job::{Job, Work};
 use self::job_queue::JobQueue;
 
-pub use self::compilation::Compilation;
+pub use self::compilation::{Compilation, CommandType};
 pub use self::context::{Context, Unit};
-pub use self::command::{CommandPrototype, CommandType};
 pub use self::layout::{Layout, LayoutProxy};
 pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts};
 
-mod command;
 mod compilation;
 mod context;
 mod custom_build;
@@ -270,9 +268,8 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
         }
 
         state.running(&rustc);
-        let process_builder = rustc.into_process_builder();
         try!(if json_errors {
-            process_builder.exec_with_streaming(
+            rustc.exec_with_streaming(
                 &mut |line| if !line.is_empty() {
                     Err(internal(&format!("compiler stdout is not empty: `{}`", line)))
                 } else {
@@ -293,7 +290,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
                 },
             ).map(|_| ())
         } else {
-            process_builder.exec()
+            rustc.exec()
         }.chain_error(|| {
             human(format!("Could not compile `{}`.", name))
         }));
@@ -359,7 +356,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
 
     // Add all relevant -L and -l flags from dependencies (now calculated and
     // present in `state`) to the command provided
-    fn add_native_deps(rustc: &mut CommandPrototype,
+    fn add_native_deps(rustc: &mut ProcessBuilder,
                        build_state: &BuildMap,
                        build_scripts: &BuildScripts,
                        pass_l_flag: bool,
@@ -394,7 +391,7 @@ fn load_build_deps(cx: &Context, unit: &Unit) -> Option<Arc<BuildScripts>> {
 // For all plugin dependencies, add their -L paths (now calculated and
 // present in `state`) to the dynamic library load path for the command to
 // execute.
-fn add_plugin_deps(rustc: &mut CommandPrototype,
+fn add_plugin_deps(rustc: &mut ProcessBuilder,
                    build_state: &BuildMap,
                    build_scripts: &BuildScripts)
                    -> CargoResult<()> {
@@ -417,7 +414,7 @@ fn add_plugin_deps(rustc: &mut CommandPrototype,
 
 fn prepare_rustc(cx: &Context,
                  crate_types: Vec<&str>,
-                 unit: &Unit) -> CargoResult<CommandPrototype> {
+                 unit: &Unit) -> CargoResult<ProcessBuilder> {
     let mut base = try!(process(CommandType::Rustc, unit.pkg, cx));
     build_base_args(cx, &mut base, unit, &crate_types);
     build_plugin_args(&mut base, cx, unit);
@@ -474,7 +471,7 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult<Work> {
             }
         }
         state.running(&rustdoc);
-        rustdoc.into_process_builder().exec().chain_error(|| {
+        rustdoc.exec().chain_error(|| {
             human(format!("Could not document `{}`.", name))
         })
     }))
@@ -502,7 +499,7 @@ fn root_path(cx: &Context, unit: &Unit) -> PathBuf {
 }
 
 fn build_base_args(cx: &Context,
-                   cmd: &mut CommandPrototype,
+                   cmd: &mut ProcessBuilder,
                    unit: &Unit,
                    crate_types: &[&str]) {
     let Profile {
@@ -616,8 +613,8 @@ fn build_base_args(cx: &Context,
 }
 
 
-fn build_plugin_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) {
-    fn opt(cmd: &mut CommandPrototype, key: &str, prefix: &str,
+fn build_plugin_args(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit) {
+    fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str,
            val: Option<&OsStr>)  {
         if let Some(val) = val {
             let mut joined = OsString::from(prefix);
@@ -637,7 +634,7 @@ fn build_plugin_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) {
     opt(cmd, "-C", "linker=", cx.linker(unit.kind).map(|s| s.as_ref()));
 }
 
-fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit)
+fn build_deps_args(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit)
                    -> CargoResult<()> {
     let layout = cx.layout(unit);
     cmd.arg("-L").arg(&{
@@ -658,7 +655,7 @@ fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit)
 
     return Ok(());
 
-    fn link_to(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit)
+    fn link_to(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit)
                -> CargoResult<()> {
         for (filename, linkable) in try!(cx.target_filenames(unit)) {
             if !linkable {
@@ -677,7 +674,7 @@ fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit)
 }
 
 pub fn process(cmd: CommandType, pkg: &Package,
-               cx: &Context) -> CargoResult<CommandPrototype> {
+               cx: &Context) -> CargoResult<ProcessBuilder> {
     // When invoking a tool, we need the *host* deps directory in the dynamic
     // library search path for plugins and such which have dynamic dependencies.
     let mut search_path = util::dylib_path();
index d73ec8103fa37d444a03dc2636263d1a24d0da50..71a22d4e46178f8aba847d6dccd8a4e9b666aaef 100644 (file)
@@ -98,7 +98,7 @@ fn run_unit_tests(options: &TestOptions,
             shell.status("Running", cmd.to_string())
         }));
 
-        if let Err(e) = cmd.into_process_builder().exec() {
+        if let Err(e) = cmd.exec() {
             errors.push(e);
             if !options.no_fail_fast {
                 break
@@ -175,7 +175,7 @@ fn run_doc_tests(options: &TestOptions,
             try!(config.shell().verbose(|shell| {
                 shell.status("Running", p.to_string())
             }));
-            if let Err(e) = p.into_process_builder().exec() {
+            if let Err(e) = p.exec() {
                 errors.push(e);
                 if !options.no_fail_fast {
                     return Ok(errors);
index 4d646ea3058d7b943f57d92bad4a14416b61162a..ff4583c569afeea8569e309c0bb73c62e2ff09d6 100644 (file)
@@ -5,7 +5,6 @@ pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
 pub use self::cargo_rustc::{compile_targets, Compilation, Layout, Kind, Unit};
 pub use self::cargo_rustc::{Context, LayoutProxy};
 pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig};
-pub use self::cargo_rustc::{CommandType, CommandPrototype};
 pub use self::cargo_run::run;
 pub use self::cargo_install::{install, install_list, uninstall};
 pub use self::cargo_new::{new, init, NewOptions, VersionControl};